<!--
	This file is part of ELCube.
	ELCube is free software: you can redistribute it and/or modify
	it under the terms of the GNU Affero General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	ELCube is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Affero General Public License for more details.
	You should have received a copy of the GNU Affero General Public License
	along with ELCube.  If not, see <https://www.gnu.org/licenses/>.
-->
<template>
    <span v-if="!editMode">
      <template v-if="value && value.length">{{value | formatTree(options)}}</template>
        <template v-else-if="designMode">
            {{["0-0","0-1-0"] | formatTree(options)}}
        </template>
      <span v-else class="empty"></span>
    </span>
    <a-tree-select size="small"
              v-else
              v-model="val"
              :mode="inputOptions.selectMode||'default'"
              :tree-data="options"
               tree-checkable
               :show-checked-strategy="SHOW_PARENT"
               placeholder="请选择..."
              @change="change">
    </a-tree-select>
</template>

<script>
import {TreeSelect} from 'ant-design-vue';


const findInTree = (tree,value)=>{
    for(let i in tree){
        const item = tree[i];
        if(item.key === value || item.value === value){
            return item;
        }
        if(item.children){
            let ret = findInTree(item.children, value);
            if(ret){
                return ret;
            }
        }
    }
    return undefined;
};

export default {
    props:{
        editMode: Boolean,
        value: {},
        inputOptions: {
            type:Object,
            default(){
                return {}
            }
        },
        designMode: Boolean
    },
    filters:{
        formatTree(value, options){
            if(value) {
                options = typeof options === 'string' ? JSON.parse(options) : options;
                return (Array.isArray(value) ? value : [value]).map(item => {
                    let find = options && findInTree(options, item);
                    if (find) {
                        return find.label || find.name || find.title;
                    }
                    return item;
                }).join(' , ');
            }
        }
    },
    data(){
        return {
            SHOW_PARENT:TreeSelect.SHOW_PARENT,
        }
    },
    computed:{
        options(){
            if(this.designMode){
                return [
                    {
                        "title": "华东",
                        "value": "0-0",
                        "key": "0-0",
                        "children": [
                            {
                                "title": "上海",
                                "value": "0-0-0",
                                "key": "0-0-0"
                            }
                        ]
                    },
                    {
                        "title": "华北",
                        "value": "0-1",
                        "key": "0-1",
                        "children": [
                            {
                                "title": "北京",
                                "value": "0-1-0",
                                "key": "0-1-0",
                                "disabled": true
                            },
                            {
                                "title": "天津",
                                "value": "0-1-1",
                                "key": "0-1-1"
                            },
                            {
                                "title": "河北",
                                "value": "0-1-2",
                                "key": "0-1-2"
                            }
                        ]
                    }
                ];
            }
            return this.inputOptions.optionsObject;
        },
        val:{
            get(){
                return this.value;
            },
            set(value){
                this.$emit('input',value);
            }
        }
    },
    methods:{
        change(){
            this.$emit('change',{});
        }
    }
}
</script>

<style scoped>

</style>